import {onMounted, reactive, ref, watch} from "vue";
import useApi, {type SysMenu, type SysMenuTree, useMenu} from "@/views/sys/api/SysMenuApi";

/**
 * @Author: wangjian
 * 656876071@qq.com
 * @Date: 2024-08-12 16:05:34
 * @LastEditTime: 2024-10-08 10:39:24
 * @FilePath: src/views/sys/menu/Hooks.ts
 * @Description:
 */
export const useEntrance = () => {

    const params = reactive<any>({})

    const columns: Array<any> = [
        {
            title: '菜单名称',
            dataIndex: 'menuName',
        },
        {
            title: '菜单编号',
            dataIndex: 'menuId',
            className: 'table-hidden',
        },
        {
            title: '分类',
            dataIndex: 'menuType',
        },
        {
            title: '组件',
            dataIndex: 'component',
        },
        {
            title: '路由地址',
            dataIndex: 'routeUrl',
        },
        {
            title: '参数',
            dataIndex: 'params',
        },
        {
            title: '图标',
            dataIndex: 'icon',
        },
        {
            title: '顺序',
            dataIndex: 'orderNum',
        },
        {
            title: '操作',
            dataIndex: 'operate',
        }
    ]
    const {deleteById} = useApi()
    const {getMenuTree} = useMenu()

    const loading = ref(true)

    /**
     * v2版本可以继续优化，不进行datasource赋值，传递Promise后续操作后再渲染
     */
    const query = (): Promise<Ret> => {
        loading.value = true
        console.debug("TableHooks：", params)
        return getMenuTree().then((res: any | Ret) => {
            console.debug("TableHooks：", res)
            if (res.data) {
                dataSource.value = res.data
            }
            loading.value = false
            return res
        })
    }
    const dataSource = ref<LnMemberCase[]>()

    const reset = () => {
        Object.keys(params).forEach(key => {
            params[key] = ''
        })
    }

    /**
     * 删除
     * @param id
     */
    const deleteSingle = (id: string) => {
        deleteById(id).then((res: any | Ret) => {
            if (res.code == 0) {
                message.success('删除成功')
                query()
            } else {
                message.error(res.data)
            }
        })
    }
    
    const copyMenu = (id: string) => {
        useApi().copyMenuById(id).then((res: any | Ret)=>{
            if(res.code == 0){
                message.success('复制成功')
                query()
            }else{
                message.error(res.data)
            }
        })
    }

    return {
        params,
        columns,
        deleteSingle,
        copyMenu,
        query,
        dataSource,
        loading,
        reset
    }
}
import {useSimpleForm} from "@/core/common/hooks/FormHooks";
import type {Ret} from "@/core/utils/request";
import type {LnMemberCase} from "@/views/sxln/api/LnMemberApi";
import {message} from "ant-design-vue";

export const useForm = (callback: Function) => {
    const {add, update, getById} = useApi()
    const {getCatalogTree} = useMenu()
    const PRIMARY_KEY = "menuId"

    const form = ref<SysMenu>({})

    const simpleForm = useSimpleForm(form, PRIMARY_KEY, add, update, getById, callback)

    const catalogTree = ref<SysMenuTree[]>([])
    
    watch(simpleForm.modalIsVisible, (newVal)=>{
        if(newVal){
            getCatalogTree().then((res: any | Ret) => {
                catalogTree.value = res.data
            })
        }
    })
    
    return {
        catalogTree,
        form,
        ...simpleForm
    }
}